package net.lzzy.practices.aspects;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.utils.AspectUtils;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.wed.models.ApiResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class CourseControllerAspect {
    /**
     * 定义关于老师的切点
     */
    @Pointcut("execution(* net.lzzy.practices.wed.course.CourseApiController.getTeacherCourses(..)) ||" +
            "execution(* net.lzzy.practices.wed.course.CourseApiController.add(..)) ||" +
            "execution(* net.lzzy.practices.wed.course.CourseApiController.update(..)) ||" +
            "execution(* net.lzzy.practices.wed.course.CourseApiController.openCloseCourse(..)) ||" +
            "execution(* net.lzzy.practices.wed.course.CourseApiController.delete(..))")
    private void teacherAspect() {}

    /**
     * 定义关于学生的切点
     */
    @Pointcut("execution(* net.lzzy.practices.wed.course.CourseApiController.getStudentsCourses(..))")
    private void studentAspect() {}

    /**
     * 定义关于通用的切点
     */
    @Pointcut("execution(* net.lzzy.practices.wed.course.CourseApiController.getCourses(..)) ||" +
            "execution(* net.lzzy.practices.wed.course.CourseApiController.getOpenCourses(..)) ||" +
            "execution(* net.lzzy.practices.wed.course.CourseApiController.getPageCourses(..)) ||" +
            "execution(* net.lzzy.practices.wed.course.CourseApiController.getPageOpenCourses(..))")
    private void courseAspect(){}

    @Around("courseAspect()")
    public ApiResult aroundCourse(ProceedingJoinPoint point) throws Throwable{
        String token = AspectUtils.getToken(point);
        if (AuthUtils.illegalUser(token,new int[]{User.TYPE_STUDENT,User.TYPE_TEACHER})){
            return new ApiResult(false, Messages.NO_PERMISSION.toString(), null);
        }
        return (ApiResult) point.proceed();
    }

    @Around("teacherAspect()")
    public ApiResult aroundTeacher(ProceedingJoinPoint point) throws Throwable{
        String token = AspectUtils.getToken(point);
        if (AuthUtils.illegalUser(token,new int[]{User.TYPE_STUDENT,User.TYPE_TEACHER})){
            return new ApiResult(false, Messages.NO_PERMISSION.toString(), null);
        }
        return (ApiResult) point.proceed();
    }

    @Around("studentAspect()")
    public ApiResult aroundStudent(ProceedingJoinPoint point) throws Throwable{
        String token = AspectUtils.getToken(point);
        if (AuthUtils.illegalUser(token,new int[]{User.TYPE_STUDENT,User.TYPE_TEACHER})){
            return new ApiResult(false, Messages.NO_PERMISSION.toString(), null);
        }
        return (ApiResult) point.proceed();
    }

}
